package com.shop.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletResponseAware;

import com.google.gson.Gson;
import com.shop.entity.User;

public class AjaxAction implements ServletResponseAware{

	private HttpServletResponse response;
	
	private User user;
	
	public String showJson() throws IOException {
		user = new User();
		user.setId(String.valueOf(1001));
		user.setPassword("000000");
		user.setUsername("tom");
		
		response.setContentType("application/json;charset=UTF-8");
		Gson gson = new Gson();
		PrintWriter out = response.getWriter();
		out.print(gson.toJson(user));
		out.flush();
		out.close();
		
		return null;
	}
	
	
	
	
	public String getXML() throws IOException {
		response.setContentType("text/xml;charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		out.print("<students>");
		out.print("<student id=\"001\">tom</student>");
		out.print("<student id=\"002\">jerry</student>");
		out.print("<student id=\"003\">rose</student>");
		out.print("</students>");
		
		out.flush();
		out.close();
		return null;
	}

	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	
	
	//get set
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
}
